package com.desksoft.ecommerce.helper.mall;

import java.util.Iterator;
import java.util.List;

import com.desksoft.utility.html.HtmlText;
import com.desksoft.utility.html.W3CText;

/***************************************
 * <pre>
 * 版权所有：杭州迪科软件 保留所有权利
 * 创建日期: 2012-5-1
 * 创建作者：rq
 * 文件名称：SearchItemDataHelper.java
 * 版本：1.0
 * 功能：
 * 最后修改时间：2012-5-1
 * 修改记录：
 * </pre>
 *****************************************/
public abstract class SearchItemDataHelper<E> implements Iterable<E> {

	private static final int DISPLAYINDEX = 7;
	private static final String MORE = "更多";
	private static final String DISPLAY = "隐藏";
	private static final String BACK_NAME = "收起";
	private static final String EXT_NAME = "展开";

	public static final String CATE_NAME = "分类：";
	public static final String PRICE_NAME = "价格区间：";
	public static final String BRAND_NAME = "品牌：";
	public static final String DESC_NAME = "特征属性：";

	public static final String sign = "_|_";
	public static final String idSign = "__";
	public static final String fgsign = "-";

	public static final String VENDOR_TYPE = "vendorCode";
	public static final String CATE_TYPE = "category";
	public static final String BRAND_TYPE = "brand";
	public static final String PRICE_TYPE = "price";
	public static final String DESC_TYPE = "desccode";

	/**
	 * 新品排序
	 */
	public static final String NEWPRODUCTS_TYPE = "newProducts";
	/**
	 * 价格排序
	 */
	public static final String ORDERPRICE_TYPE = "orderPrice";
	/**
	 * 销量排序
	 */
	public static final String ORDERSOLDNUM_TYPE = "orderSoldNum";
	/**
	 * 好评排序
	 */
	public static final String ORDERCOMMENT_TYPE = "orderComment";
	/**
	 * 
	 */
	public static final String UPPAGE_TYPE = "uppage";
	public static final String DOWNPAGE_TYPE = "downpage";
	public static final String MINDISTQTY_TYPE = "minDistQty";
	public static final String MINMAXPRICE_TYPE = "minmaxprice";
	public static final String KEYWORD_TYPE = "keyword";
	public static final String ORDER_DEF = "orderDef";
	public static final String PAGINATION_TYPE = "pagination";
	public static final String LISTMODE_TYPE = "listmode";
	public static final String SEARCHKEYWOD_TYPE = "searchKeyWord";

	public enum searchType {
		CATE_TYPE, BRAND_TYPE, PRICE_TYPE, DESC_TYPE
	}

	/**
	 * 
	 */
	private String title;
	private int index;
	private String type;
	private List<E> list;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	/**
	 * @param title
	 * @param index
	 * @param list
	 */
	public SearchItemDataHelper(List<E> list) {
		this.list = list;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	/**
	 * @return
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @return
	 */
	public int getIndex() {
		return index;
	}

	/**
	 * @return
	 */
	public boolean isDisplay() {
		if (null == list) {
			return Boolean.FALSE;
		}
		return list.size() > DISPLAYINDEX;
	}

	public void setTitle() {
		for (E e : this) {
			this.title = this.setTitle(e) + ":";
			break;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Iterable#iterator()
	 */
	public Iterator<E> iterator() {
		return list.iterator();
	}

	public abstract String setTitle(E e);

	/**
	 * @param t
	 * @return
	 */
	public abstract String getName(E e);

	/**
	 * @param t
	 * @return
	 */
	public abstract String getCode(E e);

	/**
	 * @param t
	 * @return
	 */
	public abstract String getCount(E e);

	/**
	 * @return
	 */
	public String getIds() {
		StringBuffer result = new StringBuffer();
		for (E e : this) {
			if (result.length() > 0)
				result.append(sign);
			result.append(this.getType() + idSign + this.getCode(e));
		}
		return result.toString();
	}

	/**
	 * @return
	 */
	public String getIdsName() {
		return this.getType() + "_idslist" + this.getIndex();
	}

	/**
	 * @return
	 */
	public W3CText getHiddenInputIds() {
		W3CText input = HtmlText.getInstance("input");
		input.addAttribute("type", "hidden");
		input.addAttribute("id", this.getIdsName());
		input.addAttribute("value", this.getIds());
		return (W3CText) input;
	}

	/**
	 * 生成HTML
	 * 
	 * @return
	 */
	public String toItemHtml() {
		if (this.getIndex() == 4) {
			return this.toMoreItemHtml() + this.getClearFix().toHtmlString();
		} else {
			return this.getItem().toHtmlString()
					+ this.getClearFix().toHtmlString();
		}

	}

	/**
	 * @return
	 */
	public W3CText getItem() {
		W3CText dl = HtmlText.getInstance("dl");
//		dl.addAttribute("style", "border:none");

		dl.addSubElement("dt").setValue(this.getTitle());

		W3CText dd1 = dl.addSubElement("dd");
		dd1.addSubElement(this.getHiddenInputIds());

		W3CText dd2 = null;
		if (this.isDisplay()) {
			// 隐藏的DIV
			dd2 = dl.addSubElement("dd");
			dd2.addSubElement("div").addAttribute("class", "meswindowmog")
					.addAttribute("id", "box" + this.getIndex())
					.addAttribute("style", "display:none");

			// 更多的按钮
			W3CText div = dl.addSubElement("div")
					.addAttribute("class", "gengduo")
					.addAttribute("style", "display:block;");
			div.addSubElement("a")
					.addAttribute("href", "javascript:void(0);")
					.addAttribute(
							"onClick",
							"openShutManager(this,'box" + this.getIndex()
									+ "',false,'" + DISPLAY + "','" + MORE
									+ "');").setValue(MORE);
		}

		int index = 0;
		for (E e : this) {
			W3CText p = null;
			if (index < DISPLAYINDEX) {
				p = dd1.addSubElement("p");
			} else {
				p = dd2.addSubElement("p");
			}
			p.addSubElement("a")
					.addAttribute("href", "javascript:void(0);")
					.addAttribute("id",
							this.getType() + idSign + this.getCode(e))
					.setValue(this.getName(e));
			p.addSubElement("span").setValue("(" + this.getCount(e) + ")");
			index++;
		}
		return dl;
	}

	/**
	 * @return
	 */
	public W3CText getClearFix() {
		return HtmlText.getInstance("div").addAttribute("class", "clearfix")
				.setValue(" ");
	}

	/**
	 * @return
	 */
	public String toMoreItemHtml() {
		W3CText div = HtmlText.getInstance("div");
		div.addAttribute("id", "box").addAttribute("style", "display:none");
		div.addSubElement(this.getItem());
		div.addSubElement(this.getClearFix());

		W3CText p = HtmlText.getInstance("p");
		p.addAttribute("class", "more");
		W3CText span = p.addSubElement("span").addAttribute("class", "more2");
		span.addSubElement("a")
				.addAttribute("href", "javascript:void(0);")
				.addAttribute(
						"onClick",
						"openShutManager(this,'box',false,'" + BACK_NAME
								+ "','" + EXT_NAME + "')").setValue(MORE);
		return div.toHtmlString() + p.toHtmlString();
	}

}
